package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type GlobalForbidMsgReq struct {
	SetAccount          string `json:"Set_Account"`
	MsgNoSpeakingTime   int64  `json:"C2CmsgNospeakingTime"`   // 不應禁言，保留單聊消息給server的發言驅動
	GroupNoSpeakingTime int64  `json:"GroupmsgNospeakingTime"` //0表示取消该帐号的群组消息禁言 , 4294967295表示该帐号被设置永久禁言
}
type GlobalForbidMsgResp struct {
	ErrorInfo string `json:"ErrorInfo"`
	ErrorCode int    `json:"ErrorCode"`
}

// GlobalForbidMsg 全局禁言/取消禁言

func (s sdk) GlobalForbidMsg(userId string, ban bool, tBan int64) error {
	reqUrl := s.BuildRestfulUrl("/openconfigsvr/setnospeaking")
	req := GlobalForbidMsgReq{}
	//tBan为0就是解除
	req = GlobalForbidMsgReq{SetAccount: userId, MsgNoSpeakingTime: 0, GroupNoSpeakingTime: tBan}

	bytes, _ := json.Marshal(req)

	global.Log.Debugf("===GlobalForbidMsg ===  reqUrl: %s, req: %s", reqUrl, string(bytes))
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		global.Log.Errorf("ban user - http IM error: %s", err)
		return err
	}

	resp := GlobalForbidMsgResp{}

	global.Log.Debugf("===GlobalForbidMsg ===  result: %s", string(result))
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Errorf("ban user - unmarshal IM resp error: %s", err)
		return err
	}

	if resp.ErrorCode != 0 {
		global.Log.Errorf("ban user - IM resp error: %s, error code: %v", resp.ErrorInfo, resp.ErrorCode)
		return err
	}

	return nil
}
